<template>
    <el-dialog :visible.sync="dialog" :title="isAdd ? '新增学期信息' : '编辑学期信息'" append-to-body width="450px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px" append-to-body>
            <el-form-item label="学年" prop="termYear">
                <el-date-picker
                  v-model="form.termYear"
                  type="year"
                  placeholder="学年" value-format="yyyy" style="width: 250px;" :disabled="true">
                </el-date-picker>
            </el-form-item>

            <el-form-item label="入学时间" prop="starttime">
                <el-date-picker
                  v-model="form.starttime"
                  type="date"
                  placeholder="选择日期" value-format="yyyy-MM-dd" style="width: 250px;" @change="getStartime">
                </el-date-picker>
            </el-form-item>


            <el-form-item label="结束时间" prop="endtime">
                <el-date-picker
                  v-model="form.endtime"
                  type="date"
                  placeholder="选择日期" value-format="yyyy-MM-dd" :picker-options="pickerOptions" style="width: 250px;" @change="getStartime">
                </el-date-picker>
            </el-form-item>


            <el-form-item label="学期" prop="termSegment" >
                <el-select v-model="form.termSegment" clearable  filterable placeholder="请选择学期" style="width: 250px;" >
                    <el-option
                        v-for="item in termS"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>


            <el-form-item label="状态" prop="status">
                <el-radio v-model="form.status" label="0">启用</el-radio>
                <el-radio v-model="form.status" label="1">禁用</el-radio>
            </el-form-item>


        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button type="text" @click="cancel">取消</el-button>
            <el-button :loading="loading" type="primary" @click="doSubmit" >确认</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { termSave } from '@/api/request'
import { validateUserId, validatPass, debounce } from '@/utils/validate'
export default {
    props: {
        isAdd: {
          type: Boolean,
          required: true
        },
    },
    data() {
        return {
            termS: [
                {
                    id: "0",
                    name: '上学期'
                },
                {
                    id: "2",
                    name: '下学期'
                },
            ],
            pickerOptions: {
                disabledDate(time) {
                    // return time.getTime() < Date.now();
                },
            },
            disabled:true,
            loading: false, dialog: false, permissions: [],
            form: {
                id:'',
                termYear:'',
                status: '0',
                endtime:'',
                starttime:'',
                termSegment:'', 
            },
            rules: {
                termYear:[
                    { required: true, trigger: 'blur',message: '请选择学年',},
                ],
                starttime: [
                    { required: true, trigger: 'blur',message: '请选择入学时间',},
                ],
                
                endtime:[
                    { required: true, trigger: 'blur', message: '请选择大于入学时间的结束时间',},
                ],
                termSegment:[
                    { required: true, trigger: 'blur', message: '请选择学期',},
                ],
            },
        }
    },
    methods: {
        cancel(){
            this.dialog  = false
        },
        doSubmit:debounce(function(){
            this.$refs['form'].validate((valid) => {
                if (valid){
                    // this.form.schType = (this.form.schType).toString()
                    let The_year = this.form.termYear,
                        the_start = this.form.starttime;

                    termSave(this.form).then(res => {
                        this.$notify({
                            title: '保存成功',
                            type: 'success',
                            duration: 2500
                        })
                        this.dialog = false
                        this.$parent.init()
                    })
                }else {
                    return false
                }

            })

        },500),
        getStartime(){
            let The_year = this.form.termYear;
            let sta = this.form.starttime,
                end = this.form.endtime;
                // console.log(sta.substring(0, 4))
            if (sta) {
                this.form.termYear = sta.substring(0, 4)
            }
            if (sta  >= end ) {
                return this.form.endtime  = ''
            }
           
        }
    },

}
</script>

<style>
/*提升样式*/
.ivu-select-dropdown, .ivu-cascader-transfer{
    z-index: 3000 !important;
}
.ivu-cascader .ivu-input {
    font-size: 13px;
    padding-left: 15px;
}
</style>

<style scoped lang="scss">
.el-form-item--mini.el-form-item, .el-form-item--small.el-form-item{
    display: inline-block;
}
</style>
